/*
 * This file is part of "Sorting Thoughts" project.
 *
 * Copyright (c) 2009 Hendrik Ebel
 * All Rights Reserved.
 *
 * Please visit http://code.google.com/p/sorting-thoughts/ for more information.
 *
 * --LICENSE NOTICE--
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 3
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; If not, see <http://www.gnu.org/licenses/>.
 * --LICENSE NOTICE--
 */
SortingThoughts.Cookie = ({

    initialize: function(){
        // Update globals with preferences or create it. 
        this.cookieData = new Mojo.Model.Cookie("comSortingThoughtPrefs");
        var oldPrefs = this.cookieData.get();
        if (oldPrefs) {
            if (oldPrefs.versionString == SortingThoughts.versionString) {
				// restore settings
                SortingThoughts.versionString = oldPrefs.versionString;
                SortingThoughts.enableNewByShake = oldPrefs.enableNewByShake;
                SortingThoughts.showDate = oldPrefs.showDate;
				SortingThoughts.firstStart = oldPrefs.firstStart;
				SortingThoughts.oldVersionString = oldPrefs.versionString;
                SortingThoughts.showNewFirst = oldPrefs.showNewFirst;
				SortingThoughts.sortByMdate = oldPrefs.sortByMdate;
				SortingThoughts.sortByVdate = oldPrefs.sortByVdate;
				SortingThoughts.sortByColor = oldPrefs.sortByColor;
				SortingThoughts.sortByTitle = oldPrefs.sortByTitle;
				SortingThoughts.enableAppLock = oldPrefs.enableAppLock;
				SortingThoughts.curLock = oldPrefs.curLock;
				SortingThoughts.curLockTwo = oldPrefs.curLockTwo;
				
                Mojo.Log.info("Prefs are up to date");
            } else {
				// set all settings for new version
				// settings of initial version
                SortingThoughts.enableNewByShake = oldPrefs.enableNewByShake;
                SortingThoughts.showDate = oldPrefs.showDate;
				SortingThoughts.firstStart = oldPrefs.firstStart;
				SortingThoughts.oldVersionString = oldPrefs.versionString;
                SortingThoughts.showNewFirst = oldPrefs.showNewFirst;
				
				// set new order settings, new in version 1.1.0
				if (SortingThoughts.versionString == "1.1.0") {
					if (!SortingThoughts.showNewFirst) {
						SortingThoughts.sortByMdate = true;
					} else {
						SortingThoughts.sortByMdate = false;
					}
					SortingThoughts.sortByVdate = false;
					SortingThoughts.sortByColor = false;
					SortingThoughts.sortByTitle = false;
					Mojo.Log.info("New prefs for version 1.1.0 are updated to default values");
				} else {
					SortingThoughts.sortByMdate = oldPrefs.sortByMdate;
					SortingThoughts.sortByVdate = oldPrefs.sortByVdate;
					SortingThoughts.sortByColor = oldPrefs.sortByColor;
					SortingThoughts.sortByTitle = oldPrefs.sortByTitle;
				}
				
				// set new app lock setting, new is version 1.2.0
				if (SortingThoughts.versionString == "1.2.0") {
					SortingThoughts.enableAppLock = false;
					SortingThoughts.curLock = "";
					SortingThoughts.curLockTwo = "";
					Mojo.Log.info("New prefs for version 1.2.0 are updated to default values");
				} else {
					// all other version read the old value
					SortingThoughts.enableAppLock = oldPrefs.enableAppLock;
					SortingThoughts.curLock = oldPrefs.curLock;
					SortingThoughts.curLockTwo = oldPrefs.curLockTwo;
				}
				
                Mojo.Log.info("Prefs are updated to new version");
				
            }
        }
        
		Mojo.Log.info("versionString " + SortingThoughts.versionString);
		Mojo.Log.info("oldVersionString " + SortingThoughts.oldVersionString);
		Mojo.Log.info("enableNewByShake " + SortingThoughts.enableNewByShake);
		Mojo.Log.info("versionString " + SortingThoughts.versionString);
		Mojo.Log.info("showDate " + SortingThoughts.showDate);
		Mojo.Log.info("showNewFirst " + SortingThoughts.showNewFirst);
		Mojo.Log.info("sortByMdate " + SortingThoughts.sortByMdate);
		Mojo.Log.info("sortByVdate " + SortingThoughts.sortByVdate);
		Mojo.Log.info("sortByColor " + SortingThoughts.sortByColor);
		Mojo.Log.info("sortByTitle " + SortingThoughts.sortByTitle);
		Mojo.Log.info("enableAppLock " + SortingThoughts.enableAppLock);
		Mojo.Log.info("curLock " + SortingThoughts.curLock);
		Mojo.Log.info("curLockTwo " + SortingThoughts.curLockTwo);
		
		
        this.storeCookie();
    },
    
    // store - function to update stored cookie with global values 
    storeCookie: function(){
        this.cookieData.put({
            versionString: SortingThoughts.versionString,
            enableNewByShake: SortingThoughts.enableNewByShake,
            showDate: SortingThoughts.showDate,
			firstStart: SortingThoughts.firstStart,
            showNewFirst: SortingThoughts.showNewFirst,
			sortByMdate: SortingThoughts.sortByMdate,
			sortByVdate: SortingThoughts.sortByVdate,
			sortByColor: SortingThoughts.sortByColor,
			sortByTitle: SortingThoughts.sortByTitle,
			enableAppLock: SortingThoughts.enableAppLock,
			curLock: SortingThoughts.curLock,
			curLockTwo: SortingThoughts.curLockTwo
		});
    }
    
});
